home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / ld / ldfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  8.6 KB  |  398 lines

  1. /* Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  2.  
  3. This file is part of GLD, the Gnu Linker.
  4.  
  5. GLD is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GLD is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GLD; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. /*
  20.  ldfile.c
  21.  
  22.  look after all the file stuff
  23.  
  24.  */
  25.  
  26. #include "bfd.h"
  27. #include "sysdep.h"
  28. #include "bfdlink.h"
  29. #include "ld.h"
  30. #include "ldmisc.h"
  31. #include "ldexp.h"
  32. #include "ldlang.h"
  33. #include "ldfile.h"
  34. #include "ldmain.h"
  35. #include "ldgram.h"
  36. #include "ldlex.h"
  37. #include "ldemul.h"
  38.  
  39. #include <ctype.h>
  40.  
  41. const char *ldfile_input_filename;
  42. boolean ldfile_assumed_script = false;
  43. const char *ldfile_output_machine_name = "";
  44. unsigned long ldfile_output_machine;
  45. enum bfd_architecture ldfile_output_architecture;
  46. search_dirs_type *search_head;
  47.  
  48. #ifndef MPW
  49. #ifdef VMS
  50. char *slash = "";
  51. #else
  52. char *slash = "/";
  53. #endif
  54. #else /* MPW */
  55. /* The MPW path char is a colon. */
  56. char *slash = ":";
  57. #endif /* MPW */
  58.  
  59. /* LOCAL */
  60.  
  61. static search_dirs_type **search_tail_ptr = &search_head;
  62.  
  63. typedef struct search_arch 
  64. {
  65.   char *name; 
  66.   struct search_arch *next;
  67. } search_arch_type;
  68.  
  69. static search_arch_type *search_arch_head;
  70. static search_arch_type **search_arch_tail_ptr = &search_arch_head;
  71.  
  72. static boolean ldfile_open_file_search
  73.   PARAMS ((const char *arch, lang_input_statement_type *,
  74.        const char *lib, const char *suffix));
  75. static FILE *try_open PARAMS ((const char *name, const char *exten));
  76.  
  77. void
  78. ldfile_add_library_path (name, cmdline)
  79.      const char *name;
  80.      boolean cmdline;
  81. {
  82.   search_dirs_type *new;
  83.  
  84.   new = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
  85.   new->next = NULL;
  86.   new->name = name;
  87.   new->cmdline = cmdline;
  88.   *search_tail_ptr = new;
  89.   search_tail_ptr = &new->next;
  90. }
  91.  
  92. /* Try to open a BFD for a lang_input_statement.  */
  93.  
  94. boolean
  95. ldfile_try_open_bfd (attempt, entry)
  96.      const char *attempt;
  97.      lang_input_statement_type *entry;
  98. {
  99.   entry->the_bfd = bfd_openr (attempt, entry->target);
  100.  
  101.   if (trace_file_tries)
  102.     info_msg ("attempt to open %s %s\n", attempt,
  103.           entry->the_bfd == NULL ? "failed" : "succeeded");
  104.  
  105.   if (entry->the_bfd != NULL)
  106.     return true;
  107.   else
  108.     {
  109.       if (bfd_get_error () == bfd_error_invalid_target)
  110.     einfo ("%F%P: invalid BFD target `%s'\n", entry->target);
  111.       return false;
  112.     }
  113. }
  114.  
  115. /* Search for and open the file specified by ENTRY.  If it is an
  116.    archive, use ARCH, LIB and SUFFIX to modify the file name.  */
  117.  
  118. static boolean
  119. ldfile_open_file_search (arch, entry, lib, suffix)
  120.      const char *arch;
  121.      lang_input_statement_type *entry;
  122.      const char *lib;
  123.      const char *suffix;
  124. {
  125.   search_dirs_type *search;
  126.  
  127.   /* If this is not an archive, try to open it in the current
  128.      directory first.  */
  129.   if (! entry->is_archive)
  130.     {
  131.       if (ldfile_try_open_bfd (entry->filename, entry))
  132.     return true;
  133.     }
  134.  
  135.   for (search = search_head;
  136.        search != (search_dirs_type *)NULL;
  137.        search = search->next) 
  138.     {
  139.       char *string;
  140.  
  141.       if (entry->dynamic && ! link_info.relocateable)
  142.     {
  143.       if (ldemul_open_dynamic_archive (arch, search, entry))
  144.         return true;
  145.     }
  146.  
  147.       string = (char *) xmalloc (strlen (search->name)
  148.                  + strlen (slash)
  149.                  + strlen (lib)
  150.                  + strlen (entry->filename)
  151.                  + strlen (arch)
  152.                  + strlen (suffix)
  153.                  + 1);
  154.  
  155.       if (entry->is_archive)
  156.     sprintf (string, "%s%s%s%s%s%s", search->name, slash,
  157.          lib, entry->filename, arch, suffix);
  158.       else if (entry->filename[0] == '/' || entry->filename[0] == '.')
  159.     strcpy (string, entry->filename);
  160.       else
  161.     sprintf (string, "%s%s%s", search->name, slash, entry->filename);
  162.  
  163.       if (ldfile_try_open_bfd (string, entry))
  164.     {
  165.       entry->filename = string;
  166.       return true;
  167.     }
  168.  
  169.       free (string);
  170.     }
  171.  
  172.   return false;
  173. }
  174.  
  175. /* Open the input file specified by ENTRY.  */
  176.  
  177. void
  178. ldfile_open_file (entry)
  179.      lang_input_statement_type *entry;
  180. {
  181.   if (entry->the_bfd != NULL)
  182.     return;
  183.  
  184.   if (! entry->search_dirs_flag)
  185.     {
  186.       if (ldfile_try_open_bfd (entry->filename, entry))
  187.     return;
  188.     }
  189.   else
  190.     {
  191.       search_arch_type *arch;
  192.  
  193.       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
  194.       for (arch = search_arch_head;
  195.        arch != (search_arch_type *) NULL;
  196.        arch = arch->next)
  197.     {
  198.       if (ldfile_open_file_search (arch->name, entry, "lib", ".a"))
  199.         return;
  200. #ifdef VMS
  201.       if (ldfile_open_file_search (arch->name, entry, ":lib", ".a"))
  202.         return;
  203. #endif
  204.     }
  205.     }
  206.  
  207.   einfo("%F%P: cannot open %s: %E\n", entry->local_sym_name);
  208. }
  209.  
  210. /* Try to open NAME; if that fails, try NAME with EXTEN appended to it.  */
  211.  
  212. static FILE *
  213. try_open (name, exten)
  214.      const char *name;
  215.      const char *exten;
  216. {
  217.   FILE *result;
  218.   char buff[1000];
  219.  
  220.   result = fopen (name, "r");
  221.   if (trace_file_tries)
  222.     {
  223.       if (result == NULL)
  224.     info_msg ("cannot find script file ");
  225.       else
  226.     info_msg ("opened script file ");
  227.       info_msg ("%s\n",name);
  228.     }
  229.  
  230.   if (result != NULL)
  231.     return result;
  232.  
  233.   if (*exten)
  234.     {
  235.       sprintf (buff, "%s%s", name, exten);
  236.       result = fopen (buff, "r");
  237.       if (trace_file_tries)
  238.     {
  239.       if (result == NULL)
  240.         info_msg ("cannot find script file ");
  241.       else
  242.         info_msg ("opened script file ");
  243.       info_msg ("%s\n", buff);
  244.     }
  245.     }
  246.  
  247.   return result;
  248. }
  249.  
  250. /* Try to open NAME; if that fails, look for it in any directories
  251.    specified with -L, without and with EXTEND apppended.  */
  252.  
  253. FILE *
  254. ldfile_find_command_file (name, extend)
  255.      const char *name;
  256.      const char *extend;
  257. {
  258.   search_dirs_type *search;
  259.   FILE *result;
  260.   char buffer[1000];
  261.  
  262.   /* First try raw name */
  263.   result = try_open(name,"");
  264.   if (result == (FILE *)NULL) {
  265.     /* Try now prefixes */
  266.     for (search = search_head;
  267.      search != (search_dirs_type *)NULL;
  268.      search = search->next) {
  269.       sprintf(buffer,"%s/%s", search->name, name);
  270.       result = try_open(buffer, extend);
  271.       if (result)break;
  272.     }
  273.   }
  274.   return result;
  275. }
  276.  
  277. void
  278. ldfile_open_command_file (name)
  279.      const char *name;
  280. {
  281.   FILE *ldlex_input_stack;
  282.   ldlex_input_stack = ldfile_find_command_file(name, "");
  283.  
  284.   if (ldlex_input_stack == (FILE *)NULL) {
  285.     bfd_set_error (bfd_error_system_call);
  286.     einfo("%P%F: cannot open linker script file %s: %E\n",name);
  287.   }
  288.   lex_push_file(ldlex_input_stack, name);
  289.   
  290.   ldfile_input_filename = name;
  291.   lineno = 1;
  292.   had_script = true;
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299. #ifdef GNU960
  300. static
  301. char *
  302. gnu960_map_archname( name )
  303. char *name;
  304. {
  305.   struct tabentry { char *cmd_switch; char *arch; };
  306.   static struct tabentry arch_tab[] = {
  307.     "",   "",
  308.     "KA", "ka",
  309.     "KB", "kb",
  310.     "KC", "mc",    /* Synonym for MC */
  311.     "MC", "mc",
  312.     "CA", "ca",
  313.     "SA", "ka",    /* Functionally equivalent to KA */
  314.     "SB", "kb",    /* Functionally equivalent to KB */
  315.     NULL, ""
  316.   };
  317.   struct tabentry *tp;
  318.   
  319.  
  320.   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
  321.     if ( !strcmp(name,tp->cmd_switch) ){
  322.       break;
  323.     }
  324.   }
  325.  
  326.   if ( tp->cmd_switch == NULL ){
  327.     einfo("%P%F: unknown architecture: %s\n",name);
  328.   }
  329.   return tp->arch;
  330. }
  331.  
  332.  
  333.  
  334. void
  335. ldfile_add_arch(name)
  336. char *name;
  337. {
  338.   search_arch_type *new =
  339.     (search_arch_type *)xmalloc((bfd_size_type)(sizeof(search_arch_type)));
  340.  
  341.  
  342.   if (*name != '\0') {
  343.     if (ldfile_output_machine_name[0] != '\0') {
  344.       einfo("%P%F: target architecture respecified\n");
  345.       return;
  346.     }
  347.     ldfile_output_machine_name = name;
  348.   }
  349.  
  350.   new->next = (search_arch_type*)NULL;
  351.   new->name = gnu960_map_archname( name );
  352.   *search_arch_tail_ptr = new;
  353.   search_arch_tail_ptr = &new->next;
  354.  
  355. }
  356.  
  357. #else    /* not GNU960 */
  358.  
  359.  
  360. void
  361. ldfile_add_arch (in_name)
  362.      CONST char * in_name;
  363. {
  364.   char *name = buystring(in_name);
  365.   search_arch_type *new =
  366.     (search_arch_type *) xmalloc (sizeof (search_arch_type));
  367.  
  368.   ldfile_output_machine_name = in_name;
  369.  
  370.   new->name = name;
  371.   new->next = (search_arch_type*)NULL;
  372.   while (*name) {
  373.     if (isupper(*name)) *name = tolower(*name);
  374.     name++;
  375.   }
  376.   *search_arch_tail_ptr = new;
  377.   search_arch_tail_ptr = &new->next;
  378.  
  379. }
  380. #endif
  381.  
  382. /* Set the output architecture */
  383. void
  384. ldfile_set_output_arch (string)
  385.      CONST char *string;
  386. {
  387.   const bfd_arch_info_type *arch = bfd_scan_arch(string);
  388.  
  389.   if (arch) {
  390.     ldfile_output_architecture = arch->arch;
  391.     ldfile_output_machine = arch->mach;
  392.     ldfile_output_machine_name = arch->printable_name;
  393.   }
  394.   else {
  395.     einfo("%P%F: cannot represent machine `%s'\n", string);
  396.   }
  397. }
  398.